package com.pig4cloud.pig.admin.constant;

import cn.hutool.core.date.DateUtil;
import java.util.concurrent.ThreadLocalRandom;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class CommonConstant {

	public static final String UTF8 = "UTF-8";

	public static final String STREAM_KEY = "news_queue";

	public static Date getDay() {
		Date date = new Date();
		// 生成 10（包含）- 21（包含）的随机数
		int hours = ThreadLocalRandom.current().nextInt(10, 22);
		// 生成 10（包含）-59（包含）的随机数
		int second = ThreadLocalRandom.current().nextInt(10, 60);
		String dateFormat = "yyyy-MM-dd "+hours+":"+second+":00";
		String format = DateUtil.format(date, dateFormat);
		return DateUtil.parse(format);
	}

	public static Map<String, Date> getTimeRange(String label) {
		Map<String, Date> timeRange = new HashMap<>();
		Calendar calendar = Calendar.getInstance();

		switch (label) {
			case "今天":
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				calendar.set(Calendar.MILLISECOND, 0);
				timeRange.put("start", calendar.getTime());

				calendar.set(Calendar.HOUR_OF_DAY, 23);
				calendar.set(Calendar.MINUTE, 59);
				calendar.set(Calendar.SECOND, 59);
				calendar.set(Calendar.MILLISECOND, 999);
				timeRange.put("end", calendar.getTime());
				break;
			case "本周":
				calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				calendar.set(Calendar.MILLISECOND, 0);
				timeRange.put("start", calendar.getTime());

				calendar.add(Calendar.DAY_OF_WEEK, 6);
				calendar.set(Calendar.HOUR_OF_DAY, 23);
				calendar.set(Calendar.MINUTE, 59);
				calendar.set(Calendar.SECOND, 59);
				calendar.set(Calendar.MILLISECOND, 999);
				timeRange.put("end", calendar.getTime());
				break;
			case "本月":
				calendar.set(Calendar.DAY_OF_MONTH, 1);
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				calendar.set(Calendar.MILLISECOND, 0);
				timeRange.put("start", calendar.getTime());

				calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
				calendar.set(Calendar.HOUR_OF_DAY, 23);
				calendar.set(Calendar.MINUTE, 59);
				calendar.set(Calendar.SECOND, 59);
				calendar.set(Calendar.MILLISECOND, 999);
				timeRange.put("end", calendar.getTime());
				break;
			case "本季度":
				int month = calendar.get(Calendar.MONTH);
				int startMonth = (month / 3) * 3;
				calendar.set(Calendar.MONTH, startMonth);
				calendar.set(Calendar.DAY_OF_MONTH, 1);
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				calendar.set(Calendar.MILLISECOND, 0);
				timeRange.put("start", calendar.getTime());

				calendar.add(Calendar.MONTH, 3);
				calendar.add(Calendar.DAY_OF_MONTH, -1);
				calendar.set(Calendar.HOUR_OF_DAY, 23);
				calendar.set(Calendar.MINUTE, 59);
				calendar.set(Calendar.SECOND, 59);
				calendar.set(Calendar.MILLISECOND, 999);
				timeRange.put("end", calendar.getTime());
				break;
			case "本年度":
				calendar.set(Calendar.MONTH, Calendar.JANUARY);
				calendar.set(Calendar.DAY_OF_MONTH, 1);
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				calendar.set(Calendar.MILLISECOND, 0);
				timeRange.put("start", calendar.getTime());

				calendar.set(Calendar.MONTH, Calendar.DECEMBER);
				calendar.set(Calendar.DAY_OF_MONTH, 31);
				calendar.set(Calendar.HOUR_OF_DAY, 23);
				calendar.set(Calendar.MINUTE, 59);
				calendar.set(Calendar.SECOND, 59);
				calendar.set(Calendar.MILLISECOND, 999);
				timeRange.put("end", calendar.getTime());
				break;
			default:
				throw new IllegalArgumentException("不支持的时间范围标签: " + label);
		}

		return timeRange;
	}

}
